package dataStructure;


import bean.People;

/**
 * @author xuxin
 * 环形链表
 */
public class CircleList {
    public static void main(String[] args) {
        CircleList circleList = new CircleList();
        circleList.show();
        System.out.println(circleList.getLast());
    }

    /**
     * @Author xuxin
     * @Description //TODO
     * @Date
     * @Param
     * @return
     **/
    public People head = new People(-1, "null");

    /**
     * 新增
     */
    public void add(People people) {
        //如果head是空的话，赋值给第一个节点
        if (this.head.getNo() == -1) {
            this.head = people;
            this.head.setNext(this.head);
        } else {
            People temp = this.head;
            while (temp.getNext() != this.head) {
                temp = temp.getNext();
            }
            temp.setNext(people);
            people.setNext(this.head);
        }
    }

    /**
     * 找到最后一个节点
     */
    public People getLast() {
        People result = head;
        if (result.getNo() == -1) {
            return null;
        }
        while (true) {
            if (result.getNext().equals(this.head)) {
                return result;
            }
            result = result.getNext();
        }
    }

    /**
     * 打印节点
     */
    public void show() {
        People temp = this.head;
        boolean flag = true;
        while (flag) {
            if (temp == null) {
                System.out.println("空链表");
                flag = false;
            } else {
                System.out.println(temp);
                if (temp.getNext() == this.head) {
                    flag = false;
                } else {
                    temp = temp.getNext();
                }
            }
        }
    }
}
